﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using AvalonDock;
using CompositeWPFContrib.Composite.Wpf.Regions;
using log4net;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Presentation.Regions;
using Microsoft.Practices.Composite.UnityExtensions;
using Microsoft.Practices.Unity;
using OpenExpressApp.Module.WPF.SystemModule;
using OpenExpressApp.Host.WPF;
using OpenExpressApp.Module;
using System.Reflection;

namespace OpenExpressApp
{
    /// <summary>
    /// 应用程序启动器
    /// </summary>
    internal class Bootstrapper : UnityBootstrapper
    {
        /// <summary>
        /// 获取所有需要装载的“模块”
        /// </summary>
        /// <returns></returns>
        protected override IModuleCatalog GetModuleCatalog()
        {
            ModuleCatalog catalog = new ModuleCatalog();

            //制定装载OpenExpressApp模块
            catalog.AddModule(typeof(ExpressAppModule));

            //从目录装载
            string _modulePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Module");
            Directory.CreateDirectory(_modulePath);
            var dirCatalog = new DirectoryModuleCatalog() { ModulePath = _modulePath };
            dirCatalog.Initialize();
            foreach (var module in dirCatalog.Modules)
            {
                catalog.AddModule(module);
            }

            //从配置文件读取
            var configCatalog = new ConfigurationModuleCatalog();
            configCatalog.Initialize();
            foreach (var module in configCatalog.Modules)
            {
                catalog.AddModule(module);
            }

            //等业务模块加载完毕在加载OpenExpressApp.Module.WPF模块。否则提前创建的DefaultOpen模块没有按钮。
            catalog.AddModule(typeof(WPFModule));

            return catalog;
        }

        protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
        {
            RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();

            //配置以下三种类型的RegionAdapter
            mappings.RegisterMapping(typeof(DockableContent), this.Container.Resolve<DockableContentRegionAdapter>());
            //mappings.RegisterMapping(typeof(DockablePane), this.Container.Resolve<DockablePaneRegionAdapter>());
            mappings.RegisterMapping(typeof(DocumentPane), this.Container.Resolve<DocumentPaneRegionAdapter>());
            mappings.RegisterMapping(typeof(Window), this.Container.Resolve<WindowRegionAdapter>());

            return mappings;
        }

        /// <summary>
        /// 创建第一个窗体（shell）
        /// </summary>
        /// <returns></returns>
        protected override DependencyObject CreateShell()
        {
            var shell = new Shell();
            shell.Show();           
            return shell;
        }
    }
}
